<div class="easyui-layout" data-options="fit:true">
    <!-- Begin of toolbar -->
    <div id="wu-toolbar-dm">
        <div class="wu-toolbar-button">
            <a href="#" class="easyui-linkbutton" iconCls="icon-add" onclick="openAddDm()" plain="true">添加</a>
            <a href="#" class="easyui-linkbutton" iconCls="icon-edit" onclick="openEditDm()" plain="true">修改</a>
            <a href="#" class="easyui-linkbutton" iconCls="icon-remove" onclick="removeDm()" plain="true">删除</a>
            <a href="#" class="easyui-linkbutton" iconCls="icon-remove" onclick="reloadCm()" plain="true">刷新</a>
        </div>
    </div>
    <div class="wu-toolbar-search">
        <label>起始时间：</label>
        <input class="easyui-datebox" style="width:100px" id="start-time" data-options="editable:false">
        <label>结束时间：</label>
        <input class="easyui-datebox" style="width:100px" id="end-time" data-options="editable:false">
        <a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="searchDm()">开始检索</a>
    </div>
    <!-- End of toolbar -->
    <table id="wu-datagrid-dm" class="easyui-datagrid" toolbar="#wu-toolbar-dm"></table>
</div>
<!-- Begin of easyui-dialog -->
<div id="wu-dialog-dm" class="easyui-dialog" data-options="closed:true,iconCls:'icon-save'" style="width:400px; padding:10px;">
    <form id="wu-form-dm" method="post">
        <table>
            <tr>
                <td width="60" align="right">值班时间:</td>
                <td>
                    <input type="text" id="date-dm" class="easyui-datebox" required="required" name="addtime" data-options="editable:false">
                </td>
            </tr>
            <tr>
                <td align="right">值班人:</td>
                <td>
                    <input type="text" name="content" class="wu-text" required="true"/>
                </td>
            </tr>
            <input type="text" name="id" style="display:none;">
        </table>
    </form>
</div>
<!-- End of easyui-dialog -->
<script type="text/javascript">
    // 时间默认过滤器
    $.fn.datebox.defaults.formatter = function (date) {
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        var d = date.getDate();
        var hour = date.getHours().toString();
        var minutes = date.getMinutes().toString();
        var seconds = date.getSeconds().toString();
        if (hour < 10) {
            hour = "0" + hour;
        }
        if (minutes < 10) {
            minutes = "0" + minutes;
        }
        if (seconds < 10) {
            seconds = "0" + seconds;
        }
        return y + '-' + m + '-' + d + " " + hour + ":" + minutes + ":" + seconds;
    }

    var formatter = function (date) {
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        var d = date.getDate();
        var hour = date.getHours().toString();
        var minutes = date.getMinutes().toString();
        var seconds = date.getSeconds().toString();
        if (hour < 10) {
            hour = "0" + hour;
        }
        if (minutes < 10) {
            minutes = "0" + minutes;
        }
        if (seconds < 10) {
            seconds = "0" + seconds;
        }
        return y + '-' + m + '-' + d + " " + hour + ":" + minutes + ":" + seconds;
    }

    setTimeout(function () {
        $('#start-time').datebox('setValue', $.fn.datebox.defaults.formatter(new Date((new Date()).getFullYear(),
            (new Date()).getMonth(), 1)));
        $('#end-time').datebox('setValue', $.fn.datebox.defaults.formatter(new Date((new Date()).getFullYear(),
            (new Date()).getMonth() + 1, 0)));
    }, 0);

    function searchDm() {
        var start_time = $('#start-time').datebox('getValue');
        var end_time = $('#end-time').datebox('getValue');
        $('#wu-datagrid-dm').datagrid('load', {
            starupTime: start_time,
            endTime: end_time
        });
    }

    /**
     * Name 添加记录
     */
    function addDm() {
        $('#wu-form-dm').form('submit', {
            url: $.webConfig.apiUrl + 'webgg/addWebgg',
            success: function (data) {
                var data = JSON.parse(data);
                if (data['return_code']) {
                    $.messager.alert('信息提示', '提交成功！', 'info');
                    $('#wu-dialog-dm').dialog('close');
                    $('#wu-datagrid-dm').datagrid('reload');
                } else {
                    $.messager.alert('信息提示', '提交失败！', 'info');
                }
            }
        });
    }

    /**
     * Name 修改记录
     */
    function editDm() {
        $('#wu-form-dm').form('submit', {
            url: $.webConfig.apiUrl + 'webgg/updateWebgg',
            success: function (data) {
                var data = JSON.parse(data);
                if (data['return_code'] == 1) {
                    $.messager.alert('信息提示', '提交成功！', 'info');
                    $('#wu-dialog-dm').dialog('close');
                    $('#wu-datagrid-dm').datagrid('reload');
                } else {
                    $.messager.alert('信息提示', '提交失败！', 'info');
                }
            }
        });
    }

    /**
     * Name 删除记录
     */
    function removeDm() {
        var items = $('#wu-datagrid-dm').datagrid('getSelections');
        if (items.length==0) {
            $.messager.alert('信息提示', '你没选中哦...', 'info');
            return;
        }
        $.messager.confirm('信息提示', '确定要删除该记录？', function (result) {
            if (result) {
                var items = $('#wu-datagrid-dm').datagrid('getSelections');
                var ids = [];
                $(items).each(function () {
                    ids.push(this.id);
                });

                if (!items) {
                    $.messager.alert('信息提示', '你没选中哦...', 'info');
                    return;
                }

                //alert(ids);return;
                $.ajax({
                    type: 'post',
                    url: $.webConfig.apiUrl + 'webgg/delWebgg',
                    data: {
                        id: ids[0]
                    },
                    success: function (data) {
                        var data = JSON.parse(data);
                        if (data['return_code'] == 1) {
                            $.messager.alert('信息提示', '删除成功！', 'info');
                            $('#wu-datagrid-dm').datagrid('reload');
                        } else {
                            $.messager.alert('信息提示', '删除失败！', 'info');
                        }
                    }
                });
            }
        });
    }

    /**
     * Name 打开添加窗口
     */
    function openAddDm() {
        $('#wu-form-dm').form('clear');
        $('#wu-dialog-dm').dialog({
            closed: false,
            modal: true,
            title: "添加信息",
            cache: false,
            buttons: [{
                text: '确定',
                iconCls: 'icon-ok',
                handler: addDm
            }, {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    $('#wu-dialog-dm').dialog('close');
                }
            }]
        });
    }

    /**
     * Name 打开修改窗口
     */
    function openEditDm() {
        $('#wu-form-dm').form('clear');
        var item = $('#wu-datagrid-dm').datagrid('getSelected');

        if (!item) {
            $.messager.alert('信息提示', '你没选中哦...', 'info');
            return;
        }

        item.addtime = dataFormate(item.addtime);

        $('#wu-form-dm').form('load', item);

        $('#wu-dialog-dm').dialog({
            closed: false,
            modal: true,
            title: "修改信息",
            cache: false,
            buttons: [{
                text: '确定',
                iconCls: 'icon-ok',
                handler: editDm
            }, {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    $('#wu-dialog-dm').dialog('close');
                }
            }]
        });
    }

    /**
     * Name 分页过滤器
     */
    function pagerFilter(data) {
        var pageData = {
            total: 18,
            rows: []
        };

        if (data['return_code'] == 1) {
            if (data['total']) {
                pageData['total'] = data['total'];
            }
            pageData['rows'] = data['return_body'];
        }

        return pageData;
    }


    /**
     * Name 载入数据
     */
    $('#wu-datagrid-dm').datagrid({
        queryParams: {
            starupTime: formatter(new Date((new Date()).getFullYear(), (new Date()).getMonth(), 1)),
            endTime: formatter(new Date((new Date()).getFullYear(), (new Date()).getMonth() + 1, 0))
        },
        url: $.webConfig.apiUrl + 'webgg/getWebggByTime',
        loadFilter: pagerFilter,
        rownumbers: true,
        singleSelect: true,
        pageSize: 20,
        pagination: false,
        multiSort: true,
        fitColumns: true,
        fit: true,
        columns: [
            [{
                    checkbox: true
                },
                {
                    field: 'addtime',
                    title: '值班时间',
                    width: 100,
                    sortable: true,
                    formatter: function (value, row, index) {
                        return dataFormate(value);
                    }
                },
                {
                    field: 'content',
                    title: '值班人',
                    width: 180,
                    sortable: true
                }
            ]
        ]
    });
    // 时间格式化
    function dataFormate(value) {
        var date = new Date(value);
        var year = date.getFullYear().toString();
        var month = (date.getMonth() + 1);
        var day = date.getDate().toString();
        var hour = date.getHours().toString();
        var minutes = date.getMinutes().toString();
        var seconds = date.getSeconds().toString();
        if (month < 10) {
            month = "0" + month;
        }
        if (day < 10) {
            day = "0" + day;
        }
        if (hour < 10) {
            hour = "0" + hour;
        }
        if (minutes < 10) {
            minutes = "0" + minutes;
        }
        if (seconds < 10) {
            seconds = "0" + seconds;
        }
        return year + "-" + month + "-" + day+ " " + hour + ":" + minutes + ":" + seconds;
    }

    // 刷新页面
    function reloadCm() {
        $('#wu-datagrid-dm').datagrid('reload');
    }
</script>